/*

    This file is part of GLua

    GLua Development Environment
    Copyright (C) 2007 Marine and VoiDeD

    GLua is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    GLua is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using System.IO;

using GLuaR.Classes.Workspace;

using System.Xml.Serialization;

namespace GLuaR.Classes
{
    /// <summary>
    /// Represents a folder of files
    /// </summary>
    public class Folder
    {
        /// <summary>
        /// Folder Name (Shown in the project view)
        /// </summary>
        public string Name;

        /// <summary>
        /// Parent Folder, null if none.
        /// </summary>
        [XmlIgnore]
        public Folder Parent;

        /// <summary>
        /// List of folders within the folder
        /// </summary>
        public List<Folder> Folders;

        /// <summary>
        /// Treeview Node
        /// </summary>
        [XmlIgnore]
        public TreeNode Node;

        /// <summary>
        /// List of files within the folder
        /// </summary>
        public List<OpenedFile> Files;

        /// <summary>
        /// Project the folder is in
        /// </summary>
        [XmlIgnore]
        public Project project;

        /// <summary>
        /// Workspace
        /// </summary>
        [XmlIgnore]
        public Workspace.Workspace workspace;

        public Folder ( )
        {
            Folders = new List<Folder>( );
            Files = new List<OpenedFile>( );
        }

        public string FullName ( )
        {
            string filepath = project.Path;

            // Recurse through the folders (parents) until the parent is null, then work our way back through
            Folder ff = this;

            List<Folder> fldrList = new List<Folder>( );

            while ( ff != null )
            {
                fldrList.Add( ff );
                ff = ff.Parent;
            }

            for ( int i = fldrList.Count - 1; i >= 0; i-- )
            {
                if ( ff == this )
                {
                    break;
                }
                else
                {
                    ff = fldrList[ i ];
                    filepath += "\\" + ff.Name;
                }
            }
            return filepath;
        }

        public void BuildTreeview ( Workspace.Workspace work, Project proj )
        {
            project = proj;
            
            foreach ( Folder f in Folders )
            {
                TreeNode tn_ = new TreeNode( );
                tn_.Text = f.Name;
                tn_.ContextMenuStrip = work.folderStrip;
                tn_.ImageKey = "project_folder_closed";
                tn_.SelectedImageKey = "project_folder_closed";
                tn_.Tag = f;
                f.Node = tn_;
                Node.Nodes.Add( tn_ );

                f.BuildTreeview( work, proj );

            }

            foreach ( OpenedFile pf in Files )
            {
                string filepath = FullName();

                filepath += "\\";

                if ( !File.Exists( filepath + pf.Name ) )
                {
                    pf.Valid = false;
                    pf.Saved = true;
                    pf.Folder = this;
                    TreeNode tn_ = new TreeNode( );
                    tn_.Text = pf.Name;
                    tn_.ContextMenuStrip = work.codeStrip;
                    tn_.ImageKey = "file_lost";
                    tn_.SelectedImageKey = "file_lost";
                    tn_.Tag = pf;
                    pf.Node = tn_;
                    Node.Nodes.Add( tn_ );
                    continue;
                }

                pf.FullName = filepath + pf.Name;
                pf.Saved = true;
                pf.Valid = true;
                pf.Folder = this;

                TreeNode tn2 = new TreeNode( );
                tn2.Text = pf.Name;
                tn2.ContextMenuStrip = work.codeStrip;
                tn2.ImageKey = "code";
                tn2.SelectedImageKey = "code";
                tn2.Tag = pf;
                pf.Node = tn2;

                Node.Nodes.Add( tn2 );
            }
        }

        /// <summary>
        /// Recurses through all files and checks if they are saved, then recurses through all folders and does the same...
        /// </summary>
        /// <returns>True if everything is saved, otherwise false</returns>
        public bool AllFilesSaved ( )
        {
            foreach ( Folder f in Folders )
            {
                if ( !f.AllFilesSaved() )
                    return false;
            }

            foreach ( OpenedFile of in Files )
            {
                if ( !of.Saved )
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Save's all files
        /// </summary>
        public void Save ( )
        {
            foreach ( Folder f in Folders )
            {
                f.Save( );
            }
            foreach ( OpenedFile f in Files )
            {
                f.Save( );
            }
        }

        public bool ContainsFile ( string name )
        {
            foreach ( Folder f in Folders )
            {
                if( f.ContainsFile( name ) )
                    return true;
            }
            foreach ( OpenedFile of in Files )
            {
                if( of.Name == name )
                    return true;
            }
            return false;
        }

        public void RebuildParents( Folder parentFolder, bool rootFolder )
        {
            if( rootFolder == true )
                Parent = null;
            else
                Parent = parentFolder;

            foreach( Folder folder in Folders )
                folder.RebuildParents( this, false );
        }


    }
}
